package de.uni_stuttgart.ipvs.ids.rmi;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import de.uni_stuttgart.ipvs.ids.summation.ISummationSessionFactory;
import de.uni_stuttgart.ipvs.ids.summationImpl.SummationSessionFactory;

/**
 * A RMI service offering the computation of sums.
 * @author Team 3/4 - Elkhan Dadashov, Dhaval A Shah, Patrick Stortz
 */
public class RmiServer {

	public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
		// check number of arguments and extract them
		if (args.length != 2) {
			System.err.println("Wrong number of arguments. Usage: RmiServer <registryHost> <objectName>");
			System.exit(-1);
		}
		String url = "//" + args[0] + "/" + args[1];
		// set the security manager
		if (System.getSecurityManager () == null) {
			System.setSecurityManager (new java.rmi.RMISecurityManager ());
		}

		try {
			System.out.println("Binding server at registry '" + url + "'...");
			// export the factory
			SummationSessionFactory sessionFactory = new SummationSessionFactory();
			ISummationSessionFactory sessionFactoryStub = (ISummationSessionFactory) UnicastRemoteObject.exportObject(sessionFactory, 0);
			// bind the object to the registry
			java.rmi.Naming.rebind (url, sessionFactoryStub);
			System.out.println("Server has been bound.");
		} catch (Exception ex) {
			System.err.println (ex.getMessage ());
			ex.printStackTrace ();
		}

	}
}
